﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Employee : Page
    {
        private static DataTable _dt;
        private static SqlDataAdapter _adapter;
        private static SqlConnection _conn;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGridView();
            }
        }

        public void FillGridView()
        {
            try
            {
                string cnString = ConfigurationManager.ConnectionStrings["dbPaving"].ConnectionString;
                _conn = new SqlConnection(cnString);
                _adapter = new SqlDataAdapter("select * from tbl_user", _conn);
                _dt = new DataTable();
                _adapter.Fill(_dt);
                GridView1.DataSource = _dt;
                GridView1.DataBind();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        protected void btnAddNewRecord_Click(object sender, EventArgs e)
        {
            try
            {
                if (_dt.Rows.Count > 0)
                {
                    GridView1.EditIndex = -1;
                    GridView1.ShowFooter = true;
                    FillGridView();
                }
                else
                {
                    GridView1.ShowFooter = true;
                    DataRow dr = _dt.NewRow();
                    dr[1] = "0";
                    dr[2] = 0;
                    dr[3] = 0;
                    dr[4] = "0";
                    _dt.Rows.Add(dr);
                    GridView1.DataSource = _dt;
                    GridView1.DataBind();
                    GridView1.Rows[0].Visible = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                string strUserTypeId = _dt.Rows[0]["user_type_id"].ToString();
                if (strUserTypeId == "0")
                {
                    _dt.Rows[0].Delete();
                    _adapter.Update(_dt);
                }

                var txtNewUserTypeId = GridView1.FooterRow.FindControl("txtNewUserTypeId") as TextBox;
                var txtNewName = GridView1.FooterRow.FindControl("txtNewName") as TextBox;
                var txtNewLoginName = GridView1.FooterRow.FindControl("txtNewLoginName") as TextBox;
                var txtNewPassword = GridView1.FooterRow.FindControl("txtNewPassword") as TextBox;

                var dr = _dt.NewRow();
                dr["user_id"] = 1;
                dr["user_type_id"] = txtNewUserTypeId.Text.Trim();
                dr["user_fullname"] = txtNewName.Text.Trim();
                dr["user_login_name"] = txtNewLoginName.Text.Trim();
                dr["user_password"] = txtNewPassword.Text.Trim();
                dr["create_date"] = DateTime.Now;
                dr["create_by"] = 0;
                dr["update_date"] = DateTime.Now;
                dr["update_by"] = 0;
                _dt.Rows.Add(dr);
                
                _adapter.InsertCommand = new SqlCommand("INSERT INTO tbl_user (user_type_id,user_fullname, user_login_name,user_password,create_date,create_by,update_date,update_by)"+
                string.Format(" VALUES ({0}, '{1}','{2}','{3}',{4},{5},{6},{7})", dr["user_type_id"], dr["user_fullname"], dr["user_login_name"], dr["user_password"], dr["create_date"], dr["create_by"], dr["update_date"], dr["update_by"]));
                _adapter.InsertCommand.Connection = _conn;
                _conn.Open();
                _adapter.InsertCommand.ExecuteNonQuery();
                GridView1.ShowFooter = false;
                FillGridView();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _conn.Close();
            }finally
            {
                _conn.Close();
            }
        }

        protected void btnAddNewCancel_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = false;
            FillGridView();
        }

        protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                _dt.Rows[GridView1.Rows[e.RowIndex].RowIndex].Delete();
                _adapter.Update(_dt);
                FillGridView();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected void OnRowCancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillGridView();
        }

        protected void OnRowEditingRecord(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillGridView();
        }

        protected void OnRowUpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                var id = GridView1.Rows[e.RowIndex].FindControl("lblId") as Label;
                var txtUserTypeId = GridView1.Rows[e.RowIndex].FindControl("txtUserTypeId") as TextBox;
                var txtName = GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox;
                var txtLoginName = GridView1.Rows[e.RowIndex].FindControl("txtLoginName") as TextBox;
                var txtPassword = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;

                _dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["user_type_id"] = int.Parse(txtUserTypeId.Text);
                _dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["user_fullname"] =txtName.Text.Trim();
                _dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["user_login_name"] = txtLoginName.Text.Trim();
                _dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["user_password"] = txtPassword.Text.Trim();
                string updateStr = string.Format("update tbl_user set user_fullname = '{0}', user_login_name ='{1}',user_password='{2}',user_type_id='{3}' where user_id ={4}"
                    , txtName.Text.Trim(), txtLoginName.Text.Trim(), txtPassword.Text.Trim(), txtUserTypeId.Text.Trim(), id.Text);
                _adapter.UpdateCommand = new SqlCommand(updateStr,_conn);
                _adapter.Update(_dt);
                GridView1.EditIndex = -1;
                FillGridView();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}